package org.rency.common.trigger.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.rency.common.trigger.beans.TaskStatusEnum;
import org.rency.common.trigger.schedule.DaemonContext;
import org.rency.common.trigger.schedule.DaemonTriggerBean;
import org.rency.common.trigger.schedule.repository.DaemonTaskTriggerDO;
import org.rency.common.utils.enums.YesNoEnum;

public class DaemonTriggerConverter{

	public static List<DaemonTriggerBean> convert2DaemonTriggerBean(List<DaemonTaskTriggerDO> entityList){
		if(CollectionUtils.isEmpty(entityList)){
			return null;
		}
		List<DaemonTriggerBean> beans = new ArrayList<DaemonTriggerBean>();
		for(DaemonTaskTriggerDO entity : entityList){
			beans.add(convert2DaemonTriggerBean(entity));
		}
		return beans;
	}
	
	public static DaemonTriggerBean convert2DaemonTriggerBean(DaemonTaskTriggerDO entity){
		if(entity == null){
			return null;
		}
		boolean concurrentTag = StringUtils.isBlank(entity.getConcurrentTag()) ? false : YesNoEnum.get(entity.getConcurrentTag()) == YesNoEnum.YES ? true : false;
		TaskStatusEnum status = StringUtils.isBlank(entity.getStatus()) ? TaskStatusEnum.NORMAL : TaskStatusEnum.get(entity.getStatus());
		DaemonTriggerBean bean = new DaemonTriggerBean(entity.getTriggerId(),entity.getBeanName(),entity.getDescription(),entity.getCronExpression(),
				entity.getPreviousFire(),entity.getNextFire(),status,concurrentTag,entity.getMachineIp(),entity.getMachineHost(),
				entity.getUpdateTime(),entity.getCreateTime(),entity.getMemo());
		
		return bean;
	}
	
	public static DaemonTaskTriggerDO convert2DaemonTriggerDO(DaemonTriggerBean domain){
		DaemonTaskTriggerDO entity = new DaemonTaskTriggerDO();
		entity.setBeanName(domain.getBeanName());
		entity.setConcurrentTag(domain.getConcurrentTag() == true ? YesNoEnum.YES.getCode() : YesNoEnum.NO.getCode());
		entity.setCreateTime(domain.getCreateTime());
		entity.setCronExpression(domain.getCronExpression());
		entity.setDescription(domain.getDescription());
		entity.setMachineHost(domain.getMachineHost());
		entity.setMachineIp(domain.getMachineIp());
		entity.setMemo(domain.getMemo());
		entity.setNextFire(domain.getNextFire());
		entity.setPreviousFire(domain.getPreviousFire());
		entity.setStatus(domain.getStatus().getCode());
		entity.setTriggerId(domain.getTriggerId());
		entity.setUpdateTime(domain.getUpdateTime());
		return entity;
	}
	
	public static DaemonContext convert2DaemonContext(DaemonTriggerBean domain){
		DaemonContext context = new DaemonContext();
		context.setBeanName(domain.getBeanName());
		context.setConcurrentTag(domain.getConcurrentTag());
		context.setDescription(domain.getDescription());
		context.setMemo(domain.getMemo());
		context.setNextFire(domain.getNextFire());
		context.setPreviousFire(domain.getPreviousFire());
		context.setStatus(domain.getStatus());
		context.setTriggerId(domain.getTriggerId());
		return context;
	}
}